package com.ztesoft.huangfu.learning.springboot.springboottransaction.controller;

import com.ztesoft.huangfu.learning.springboot.springbootcommon.base.BaseResponse;
import com.ztesoft.huangfu.learning.springboot.springboottransaction.entity.User;
import com.ztesoft.huangfu.learning.springboot.springboottransaction.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/5/23
 * Time: 22:59
 * Version: 1.0
 * Description: restful的增删改查，从mybaties中获取
 * 增加分页 2018-6-1 用PageHelper插件
 **/
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "新增用户", notes = "单个新增")
    @ApiImplicitParam(name = "user", value = "新增的用户实体")
    @PostMapping("/addUser")
    public int addUser(User user) {
        return userService.addUser(user);
    }

    @ApiOperation(value = "根据Id删除用户")
    @DeleteMapping("/delete")
    public int delete(String id) {
        return userService.delete(id);
    }

    @ApiOperation(value = "修改用户信息")
    @PostMapping("/update")
    public int update(User user) {
        return userService.update(user);
    }

    @ApiOperation(value = "获取所有用户信息")
    @GetMapping("/selectUsers")
    public BaseResponse<List<User>> selectUsers(int pageNum, int pageSize) {

        return new BaseResponse<>(true, "查询列表成功", userService.selectUsers(pageNum, pageSize));
    }

    @ApiOperation(value = "根据Id获取用户信息")
    @GetMapping("/selectUserById")
    public User selectUserById(String id) {
        log.info("selectUserById:" + id);
        return userService.selectUserById(id);
    }
}
